package com.dyypdadmin.controller;

import com.dyypdadmin.pojo.AppointmentRecords;
import com.dyypdadmin.pojo.vo.AppointmentRecordsDetail;
import com.dyypdadmin.pojo.vo.CategoriesCount;
import com.dyypdadmin.result.Result;
import com.dyypdadmin.service.AppointmentRecordsDetailService;
import com.dyypdadmin.service.AppointmentRecordsService;
import com.dyypdadmin.service.ServicecategoriesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/appointment")
public class AppointmentRecordsController {

    @Autowired
    private AppointmentRecordsService appointmentRecordsService;
    @Autowired
    private AppointmentRecordsDetailService appointmentRecordsDetailService;
    @Autowired
    private ServicecategoriesService servicecategoriesService;

    @PostMapping("/add")
    public Result addAppointmentRecords(@RequestBody AppointmentRecords request) {
        try {
            Integer user_pkid = request.getUser_pkid();
            Integer pet_id = request.getPet_id();
            Integer project_id = request.getProject_id();
            Integer doctor_pkid = request.getDoctor_pkid();
            Date appointment_time = request.getAppointment_time();
            Integer is_medical = request.getIs_medical();
            appointmentRecordsService.insertAppointmentRecords(user_pkid, pet_id, project_id, doctor_pkid, appointment_time,is_medical);
            return new Result(true, "预约添加成功");
        } catch (Exception e) {
            return new Result(false, "预约添加失败：" + e.getMessage());
        }
    }

    @DeleteMapping("/delete/{appointmentPkid}")
    public Result deleteAppointmentRecords(@PathVariable("appointmentPkid") Integer appointmentPkid) {
        try {
            appointmentRecordsService.deleteAppointmentRecords(appointmentPkid);
            return new Result(true, "预约删除成功");
        } catch (Exception e) {
            return new Result(false, "预约删除失败：" + e.getMessage());
        }
    }

    @PutMapping("/update")
    public Result updateAppointmentRecords(@RequestBody AppointmentRecords appointmentRecords) {
        try {
            appointmentRecordsService.updateAppointmentRecords(appointmentRecords);
            return new Result(true, "预约更新成功");
        } catch (Exception e) {
            return new Result(false, "预约更新失败：" + e.getMessage());
        }
    }

    @GetMapping("/get/{appointmentPkid}")
    public Result getAppointmentRecordsById(@PathVariable("appointmentPkid") Integer appointmentPkid) {
        try {
            AppointmentRecords appointmentRecords = appointmentRecordsService.getAppointmentRecordsById(appointmentPkid);
            return new Result(true, appointmentRecords, "预约查询成功");
        } catch (Exception e) {
            return new Result(false, "预约查询失败：" + e.getMessage());
        }
    }

    @GetMapping("/list")
    public Result getAllAppointmentRecords() {
        try {
            List<AppointmentRecords> appointmentRecordsList = appointmentRecordsService.getAllAppointmentRecords();
            return new Result(true, appointmentRecordsList, "预约记录查询成功");
        } catch (Exception e) {
            return new Result(false, "预约记录查询失败：" + e.getMessage());
        }
    }

    @GetMapping("/getDetailList")
    public Result getAllAppointmentRecordsDetails() {
        List<AppointmentRecordsDetail> appointmentRecordsList = appointmentRecordsDetailService.getAll();
        if(appointmentRecordsList!=null){
            return new Result(true,appointmentRecordsList);
        }else{
            return new Result(true,"暂时没有预约信息");
        }
    }
    /*
    * 改变订单的状态
    * 说明：
    * status：1.完成
    * 2.院方取消
    * 3.用户取消
    * */
    @GetMapping("/update/{appointment_pkid}/{status}")
    public Result updateAppointmentStatus(@PathVariable("appointment_pkid") Integer appointment_pkid,@PathVariable("status") Integer status) {
        String sta ;
        switch (status) {
            case 1:
                sta = "服务完成";
                break;
            case 2:
                sta = "院方拒绝";
                break;
            case 3:
                sta = "用户取消";
                break;
            default:
                sta = "未知状态";
                break;
        }

        int result = appointmentRecordsService.updateAppointment_status(appointment_pkid,sta);
        if(result>0){
            return new Result(true,"更新成功");
        }else{
            return new Result(false,"更新失败");
        }
    }

    /*
    * 根据用户id获取该用户所有预约信息
    * */
    @GetMapping("/getAllByUserId/{user_pkid}")
    public Result getAllByUserId(@PathVariable("user_pkid")Integer user_pkid){
        List<AppointmentRecordsDetail> appointmentRecordsList = appointmentRecordsDetailService.getAllByUserId(user_pkid);
        if(appointmentRecordsList!=null){
            return new Result(true,appointmentRecordsList);
        }else{
            return new Result(true,"暂时没有预约信息");
        }
    }
    /**
     * @description:根据预约时间获取列表
     * @author:邓盈盈
     * @date: 2024/3/7 下午 06:04
     * @param:
     * @return:
     **/
    @GetMapping("/getAppointmentListByAppointmentTime/{appointment_time}")
    public Result getAppointmentListByAppointmentTime(@PathVariable("appointment_time") Date appointment_time) {
        System.out.println("param_time"+appointment_time+appointment_time.getClass());
        // 创建 SimpleDateFormat 对象，指定目标日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        // 使用 SimpleDateFormat 格式化日期对象为字符串
        String formattedDate = dateFormat.format(appointment_time);

        // 输出格式化后的日期字符串
        System.out.println("Formatted Date String: " + formattedDate);
        List<AppointmentRecordsDetail> result = appointmentRecordsDetailService.getAppointmentListByAppointmentTime(formattedDate);
        if(result!=null){
            return new Result(true,result);
        }else{
            return new Result(false,"fail");
        }
    }
    /**
     * @description:根据用户名查询预约
     * @author:邓盈盈
     * @date: 2024/3/8 上午 11:05
     * @param: username
     * @return:
     **/
    @GetMapping("/getAppointmentListByUsername/{username}")
    public Result getAppointmentListByUsername(@PathVariable("username")String username){
        String newKeyWord = "%"+username+"%";
        List<AppointmentRecordsDetail> result = appointmentRecordsDetailService.getAppointmentListByUsername(newKeyWord);
        if(result!=null){
            return new Result(true,result);
        }else{
            return new Result(false,"fail");
        }

    }
    /**
     * @description:获取总的预约数量
     * @author:
     * @date: 2024/3/8 下午 08:51
     * @param:
     * @return:
     **/
    @GetMapping("/getAppointmentCounts")
    public Result getAppointmentCounts(){
        int result = appointmentRecordsService.getAppointmentCounts();
        return new Result(true,result);

    }
    /**
     * @description: 获取各个服务的服务数量
     * @author:
     * @date: 2024/3/8 下午 09:27
     * @param: 
     * @return: 
     **/
    @GetMapping("/getAppointmentType")
    public Result getAppointmentType(){
        List<CategoriesCount> result = appointmentRecordsDetailService.getCountByServiceType();
        System.out.println(result.get(0));
        if(result!=null){
            return new Result(true,result);
        }else{
            return new Result(false,"fail");
        }
    
    }
    /**
     * @description: 根据用户id查询待服务的预约
     * @author:
     * @date: 2024/3/30 上午 03:54
     * @param: 
     * @return: 
     **/
    @GetMapping("/getWaitServeByUserId/{user_pkid}")
    public Result getWaitServeByUserId(@PathVariable("user_pkid")Integer user_pkid){
        List<AppointmentRecordsDetail> appointmentRecordsList = appointmentRecordsDetailService.getWaitServeByUserId(user_pkid);
        if(appointmentRecordsList!=null){
            return new Result(true,appointmentRecordsList);
        }else{
            return new Result(true,"暂时没有预约信息");
        }
    
    }
    /**
     * @description: 根据用户获取对应服务完成的预约
     * @author:
     * @date: 2024/3/30 上午 03:57
     * @param: 
     * @return: 
     **/
    @GetMapping("/getCompletedByUserId/{user_pkid}")
    public Result getCompletedByUserId(@PathVariable("user_pkid")Integer user_pkid){
        List<AppointmentRecordsDetail> appointmentRecordsList = appointmentRecordsDetailService.getCompletedByUserId(user_pkid);
        if(appointmentRecordsList!=null){
            return new Result(true,appointmentRecordsList);
        }else{
            return new Result(true,"暂时没有预约信息");
        }
    
    }
    /**
     * @description: 获取取消或者拒绝的预约
     * @author:
     * @date: 2024/3/30 上午 03:59
     * @param:
     * @return:
     **/
    @GetMapping("/getIncompleteByUserId/{user_pkid}")
    public Result getIncompleteByUserId(@PathVariable("user_pkid")Integer user_pkid){
        List<AppointmentRecordsDetail> appointmentRecordsList = appointmentRecordsDetailService.getIncompleteByUserId(user_pkid);
        if(appointmentRecordsList!=null){
            return new Result(true,appointmentRecordsList);
        }else{
            return new Result(true,"暂时没有预约信息");
        }

    }
   /**
    * @description:
    * @author:
    * @date: 2024/3/30 上午 04:34
    * @param:
    * @return:
    **/
   @GetMapping("/getTodayAppointmentsCount")
   public Result getTodayAppointmentsCount(){
       int result = appointmentRecordsDetailService.getTodayAppointmentsCount();
       if(result!=-1){
           return new Result(true,result);
       }else{
           return new Result(false,"fail");
       }

   }
}
